package com.tgky.ybzb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tgky.ybzb.common.PageParam;
import com.tgky.ybzb.entity.SysDict;
import com.tgky.ybzb.utils.MPUtil;
import com.tgky.ybzb.entity.SysUser;
import com.tgky.ybzb.mapper.SysUserMapper;
import com.tgky.ybzb.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统用户 服务实现类
 *
 * @author XYP
 * @since 2024-07-22
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 通过实体类获取QueryWrapper
     * @param pageParam 分页参数
     * @param sysUser 实体信息
     * @return
     */
    @Override
    public QueryWrapper<SysUser> getQueryWrapper(PageParam pageParam, SysUser sysUser){
        QueryWrapper<SysUser> queryWrapper = MPUtil.getQueryWrapper(pageParam);

        if(sysUser == null){
            return queryWrapper;
        }

        //主键
        if(sysUser.getUserId() != null){
            queryWrapper.lambda()
                    .eq(SysUser::getUserId, sysUser.getUserId());
            return queryWrapper;
        }

        //TODO 此处可以根据各字段查询需求修改查询条件，eq、like、ge、gt、le、lt、ne...等等等
        queryWrapper.lambda()
                .eq(sysUser.getUsername() != null, SysUser::getUsername, sysUser.getUsername())
                .eq(sysUser.getRealName() != null, SysUser::getRealName, sysUser.getRealName())
                .eq(sysUser.getPassword() != null, SysUser::getPassword, sysUser.getPassword())
                .eq(sysUser.getEmail() != null, SysUser::getEmail, sysUser.getEmail())
                .eq(sysUser.getMobile() != null, SysUser::getMobile, sysUser.getMobile())
                .eq(sysUser.getUserStatus() != null, SysUser::getUserStatus, sysUser.getUserStatus())
                .eq(sysUser.getUserUnits() != null, SysUser::getUserUnits, sysUser.getUserUnits())
                .eq(sysUser.getCreateTime() != null, SysUser::getCreateTime, sysUser.getCreateTime());
        return queryWrapper;
    }

    /**
     * 获取字典值对象
     * @return
     */
    @Override
    public Map<String, List<SysDict>> getDictList() {
        List<SysDict> sysDictList=sysUserMapper.getDictList();
        Map<String,List<SysDict>> map=new HashMap<>();
        sysDictList.forEach(item->{
            if(!map.containsKey(item.getType().toString())){
                List<SysDict> sdicts=new ArrayList<>();
                sdicts.add(item);
                map.put(item.getType().toString(),sdicts);
            }
            else{
                map.get(item.getType().toString()).add(item);
            }
        });
        return map;
    }
}
